package com.example.mynotepad;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mynotepad.database.MyOpenHelper;
import com.example.mynotepad.utils.MyConstant;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView mNoteBack;
    private TextView mNoteName;
    private TextView mTvTime;
    private EditText mNoteContent;
    private ImageView mDelete;
    private ImageView mNoteSave;
    private MyOpenHelper myOpenHelper;
    private String id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.flagColor));
        setContentView(R.layout.activity_record);
        initView();
        initListener();
        // 打开数据库、数据表
        myOpenHelper = new MyOpenHelper(this);
        // 判断是添加记录还是修改记录
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        if (id != null){
            // 修改记录
            mNoteName.setText("修改记录");
            mNoteContent.setText(intent.getStringExtra("record"));
            mTvTime.setText(intent.getStringExtra("time"));
            mTvTime.setVisibility(View.VISIBLE);  // 把日期时间控件显示出来
        } else {
            // 添加记录
            mNoteName.setText("添加记录");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.note_back:
                judge_Save();
                finish(); // 结束当前界面

                break;
            case R.id.delete:
                mNoteContent.setText("");
                break;
            case R.id.note_save:
                String noteContent = mNoteContent.getText().toString().trim();
                String time = MyConstant.getTime();
                // 判断是新添加保存，还是修改后保存
                if (id != null){
                    // 修改后保存
                    if (noteContent.length() > 0) {
                        int i = myOpenHelper.updateRecord(id, noteContent, time);
                        if (i > 0){
                            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                            setResult(RESULT_OK);
                            finish();
                        }
                    }
                } else {
                    // 新添加保存
                    if (noteContent.length() > 0) {
                        long n = myOpenHelper.insertRecord(noteContent, time);
                        if (n > 0) {
                            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                            setResult(RESULT_OK);
                            finish();
                        }
                    }
                }
                break;
        }
    }

    private void initListener() {
        mNoteBack.setOnClickListener(this);
        mDelete.setOnClickListener(this);
        mNoteSave.setOnClickListener(this);
    }

    /**
     * 监听按钮
     */
    private void judge_Save(){
        String noteContent = mNoteContent.getText().toString().trim();
        String time = MyConstant.getTime();
        // 判断是新添加保存，还是修改后保存
        if (id != null){
            // 修改后保存
            if (noteContent.length() > 0) {
                int i = myOpenHelper.updateRecord(id, noteContent, time);
                if (i > 0){
                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                }
            }
        } else {
            // 新添加保存
            if (noteContent.length() > 0) {
                long n = myOpenHelper.insertRecord(noteContent, time);
                if (n > 0) {
                    Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                }
            }
        }
    }

    private void initView() {
        mNoteBack = (ImageView) findViewById(R.id.note_back);
        mNoteName = (TextView) findViewById(R.id.note_name);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mNoteContent = (EditText) findViewById(R.id.note_content);
        mDelete = (ImageView) findViewById(R.id.delete);
        mNoteSave = (ImageView) findViewById(R.id.note_save);
    }
    /**
     * 初始化界面
     */


}
